//
//  HouseLoanCalculateChartsView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/28.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

//
//  SalerInfoCommentChartView.m
//  YiJuZhangShou
//
//  Created by URANUS on 16/3/21.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "HouseLoanCalculateChartsView.h"
#import "Masonry.h"
#import "PNChart.h"

@interface HouseLoanCalculateChartsView()
@property (nonatomic, strong) PNPieChart *pnPieChart;
@property (nonatomic, strong) UIView *legendView;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation HouseLoanCalculateChartsView

#pragma mark Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadHouseLoanChartView];
    }
    return self;
}

#pragma mark Private
- (void)loadHouseLoanChartView{
    [self.pnPieChart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(TweentyOffsetViewMargin);
        make.centerX.equalTo(self.mas_centerX);
        make.height.equalTo(self.mas_height).multipliedBy(0.3);
        make.width.equalTo(self.pnPieChart.mas_height);
        
    }];
    [self.pnPieChart strokeChart];
    [self.legendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pnPieChart.mas_bottom).offset(TweentyOffsetViewMargin*2);
        make.centerX.equalTo(self.mas_centerX);
        make.width.equalTo(self.mas_width);
        make.height.equalTo(self).multipliedBy(0.5);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.pnPieChart);
    }];
}

- (void)loadSalerInfoChartLegendWithBackView:(UIView *)backView withImageArray:(NSArray *)imageArray andTitleArray:(NSArray *)titleArray{
    
    for (UIView *view in backView.subviews) {
        if (view.tag == -1) {
            [view removeFromSuperview];
        }
    }
    
    UIView *lastView = nil;
    for (int i=0; i<titleArray.count; i++) {
        UIView *view = [UIView new];
        view.tag = -1;
        [backView addSubview:view];
//        view.backgroundColor = [UIColor randomColor];
        if (lastView) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lastView.mas_bottom);
                make.left.right.equalTo(lastView);
                make.height.equalTo(backView.mas_height).multipliedBy(1/10.0f);
                if (i == (titleArray.count-1)) {
                    make.top.equalTo(lastView.mas_bottom);
                }
            }];
        }else{
           [view mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.top.right.equalTo(backView);
               make.height.equalTo(backView.mas_height).multipliedBy(1/10.0f);
           }];
        }
        
        [self loadLegendItemViewWithImageString:imageArray[i] withTitle:titleArray[i] andBackView:view];
        lastView = view;
    }
}

- (void)loadLegendItemViewWithImageString:(NSString *)imageString
                                withTitle:(NSString *)title
                              andBackView:(UIView *)backView{
    UIImageView *imageView = [UIImageView new];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [imageView setImage:[UIImage imageNamed:imageString]];
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = TextFont14;
    titleLabel.textColor = TextColor80;
    titleLabel.text = title;
    
    [backView addSubview:imageView];
    [backView addSubview:titleLabel];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView.mas_top);
        make.left.equalTo(backView.mas_left).offset(TweentyOffsetViewMargin);
        make.height.equalTo(@15);
        make.width.equalTo(imageView.mas_height);
        
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_top);
        make.left.equalTo(imageView.mas_right).offset(FiveOffsetViewMargin);
        make.width.equalTo(backView.mas_width);
        make.height.equalTo(@15);
    }];
}

- (void)setDataCount:(NSInteger)count andArray:(NSMutableArray *)commentRateArray{
    NSArray *parties = @[@"首付金额", @"贷款总额", @"贷款利息",@"优惠"];
    NSArray *imageStrings = @[@"calculateColor1",@"calculateColor2",@"calculateColor3",@"calculateColor4"];
    NSArray *colors = @[[UIColor colorWithRed:105/255.0 green:208/255.0 blue:209/255.0 alpha:1.0],[UIColor colorWithRed:249/255.0 green:142/255.0 blue:136/255.0 alpha:1.0],[UIColor colorWithRed:253/255.0 green:173/255.0 blue:50/255.0 alpha:1.0],[UIColor colorWithRed:232/255.0 green:64/255.0 blue:63/255.0 alpha:1.0]];
    NSMutableArray *items = [NSMutableArray array];
    NSMutableArray *legendItemTitles = [NSMutableArray array];
    for (int i=0; i<parties.count; i++) {
        PNPieChartDataItem *pnpieChartDataItem = [PNPieChartDataItem dataItemWithValue:[commentRateArray[i] floatValue] color:colors[i]];
        [items addObject:pnpieChartDataItem];
        if (i == parties.count - 1) {
            [legendItemTitles addObject:[NSString stringWithFormat:@"%@:%.0f元",parties[i],[commentRateArray[i] floatValue]*10000]];
        }else{
            [legendItemTitles addObject:[NSString stringWithFormat:@"%@:%.2f万元",parties[i],[commentRateArray[i] floatValue]]];
        }
        
    }
    [self.pnPieChart updateChartData:items];
    [self.pnPieChart strokeChart];
    
    [self loadSalerInfoChartLegendWithBackView:self.legendView withImageArray:imageStrings andTitleArray:legendItemTitles];
}

- (PNPieChart *)pnPieChart{
    if (_pnPieChart == nil) {
        _pnPieChart = [[PNPieChart alloc] init];
        _pnPieChart.descriptionTextColor = [UIColor whiteColor];
        _pnPieChart.descriptionTextFont  = [UIFont fontWithName:@"Avenir-Medium" size:11.0];
        _pnPieChart.descriptionTextShadowColor = [UIColor clearColor];
        _pnPieChart.showAbsoluteValues = NO;
        _pnPieChart.showOnlyValues = NO;
        _pnPieChart.legendStyle = PNLegendItemStyleStacked;
        _pnPieChart.legendFont = [UIFont boldSystemFontOfSize:16];
        
        [self addSubview:_pnPieChart];
    }
    return _pnPieChart;
}

- (UIView *)legendView{
    if (_legendView == nil) {
        _legendView = [UIView new];
        [self addSubview:_legendView];
    }
    return _legendView;
}

- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [UILabel new];
        _titleLabel.font = TextFont12;
        _titleLabel.textColor = TextColor80;
        _titleLabel.numberOfLines = 0;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

#pragma mark Setter
-(void)setModel:(HouseLoanCalculateChartsModel *)model{
    _model = model;
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:[NSNumber numberWithFloat:model.downPayment]];
    [array addObject:[NSNumber numberWithFloat:model.loanTotal]];
    [array addObject:[NSNumber numberWithFloat:model.loanInterest]];
    [array addObject:[NSNumber numberWithFloat:model.loanDiscount]];
    [self setDataCount:array.count andArray:array];
    self.titleLabel.text =[NSString stringWithFormat:@" 参考月供\n%ld元/月",(long)model.pricePerMonth];
    
}

@end
